package com.ch.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ch.web.ResponseDto.xyBackComment.BackCommentListResponse;
import com.ch.web.ResponseDto.xyBackComment.XyBackCommentResponse;
import com.ch.web.entity.user.XyBackCommentEntity;
import com.ch.web.mapper.XyBackCommentDao;
import com.ch.web.service.XyBackCommentService;
import com.ch.web.service.XyCommentService;
import com.ch.web.service.XyUserService;
import com.ch.web.utils.ColaBeanUtils;
import com.ch.web.utils.PageUtils;
import com.ch.web.utils.Query;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("xyBackCommentService")
public class XyBackCommentServiceImpl extends ServiceImpl<XyBackCommentDao, XyBackCommentEntity> implements XyBackCommentService {

    @Autowired
    XyUserService xyUserService;

    @Resource
    XyCommentService xyCommentService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<XyBackCommentEntity> queryWrapper = new QueryWrapper<>();
        String backUser = (String) params.get("backUser");
        if (StringUtils.isNotBlank(backUser)) {
            //根据回复人的名字 去查回复人对应的id
            Long userId = xyUserService.geUserIdByUserNick(backUser);
            if (ObjectUtils.isEmpty(userId)){
                return new PageUtils(new Page<BackCommentListResponse>());
            }
        }
        String targetUser = (String) params.get("targetUser");
        if (StringUtils.isNotBlank(targetUser)) {
            //根据回复人的名字 去查回复人对应的id
            Long userId = xyUserService.geUserIdByUserNick(targetUser);
            if (ObjectUtils.isEmpty(userId)){
                return new PageUtils(new Page<BackCommentListResponse>());
            }
            queryWrapper.eq("targetUserId", userId);
        }

        IPage<XyBackCommentEntity> page = this.page(
                new Query<XyBackCommentEntity>().getPage(params),
                queryWrapper
        );
        page.setRecords(page.getRecords().stream().map(xyBackCommentEntity -> {
            BackCommentListResponse response = ColaBeanUtils.copyObject(xyBackCommentEntity, BackCommentListResponse::new);
            response.setBackUserName(xyUserService.getById(response.getBackId()).getUserNick());
            response.setTargetUserName(xyUserService.getById(response.getTargetUserId()).getUserNick());
            response.setCommentInfo(xyCommentService.getById(response.getCommentId()).getComment());
            return response;
        }).collect(Collectors.toList()));
        return new PageUtils(page);
    }

    @Override
    public List<XyBackCommentResponse> getBackByCommentId(Long commentId) {
        QueryWrapper<XyBackCommentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("comment_id", commentId);
        List<XyBackCommentEntity> xyBackCommentEntities = baseMapper.selectList(wrapper);
        List<XyBackCommentResponse> xyBackCommentResponses = ColaBeanUtils.copyListProperties(xyBackCommentEntities, XyBackCommentResponse::new);
        xyBackCommentResponses.forEach(xyBackCommentResponse -> {
            //设置回复人
            xyBackCommentResponse.setBackUser(xyUserService.getById(xyBackCommentResponse.getBackId()));
            //设置回复给谁的人
            xyBackCommentResponse.setTargetUser(xyUserService.getById(xyBackCommentResponse.getTargetUserId()));
        });
        return xyBackCommentResponses;
    }

    @Override
    public boolean addBackComment(XyBackCommentEntity xyBackComment) {
        return this.save(xyBackComment);
    }

    @Override
    public void deleteBackCommentByCommentId(Long commentId) {
        QueryWrapper<XyBackCommentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("comment_id", commentId);
        this.remove(wrapper);
    }

}
